﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection(GlobalConstant.CONNECTION_STRING);

    public class Staff
    {
        public int ID
        {
            get;
            set;
        }
        public String FName
        {
            get;
            set;
        }
        public String LName
        {
            get;
            set;
        }
    }
    public static List<Staff> listStaff;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadStaff();
            StaffInfor.lockEdit(true);
        }
    }
   
    protected void Read_More(object sender, CommandEventArgs e)
    {        
        
    }

    protected void loadStaff()
    {
        listStaff = new List<Staff>();
        string sql = "SELECT ID, FirstName, LastName FROM tbStaff";
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Staff nvien = new Staff();
            nvien.ID = reader.GetInt32(0);
            nvien.FName = reader.GetString(1);
            nvien.LName = reader.GetString(2);
            listStaff.Add(nvien);
        }
        reader.Close();
        connection.Close();
        GridviewStaff.DataSource = listStaff;
        GridviewStaff.DataBind();
    }

    protected void loadStaffWithSearch()
    {
        listStaff = new List<Staff>();
        string sql = "SELECT ID, FirstName, LastName FROM tbStaff WHERE LastName LIKE "+"'%"+txtSearch.Text.Trim()+"%'";
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Staff nvien = new Staff();
            nvien.ID = reader.GetInt32(0);
            nvien.FName = reader.GetString(1);
            nvien.LName = reader.GetString(2);
            listStaff.Add(nvien);
        }
        reader.Close();
        connection.Close();
        GridviewStaff.DataSource = listStaff;
        GridviewStaff.DataBind();
    }

    public void linkbtnName_OnClicked(object sender, EventArgs e)
    {
        int id = Int32.Parse((sender as LinkButton).CommandArgument);
        StaffInfor.fillInfor(id);
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        btnEdit.Enabled = true;
        StaffInfor.UpdateData();
        Response.Redirect("~/Management/PersonnelOfficer/UpdateStaff.aspx");
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        StaffInfor.DeleteData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadStaffWithSearch();
    }
    protected void linkbtn_Click(object sender, EventArgs e)
    {
        if ((StaffInfor.FindControl("txtFirstName") as TextBox).Text != "")
        {
            StaffInfor.lockEdit(false);
        }
        else
        {
            Response.Write("<script> alert('Không thể sửa khi chưa chọn Nhân viên'); </script>");
        }
    }
}
